<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="false">
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectname" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.projectname" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openmpmProjectRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目编号" prop="projectcode">
            <el-input v-model="model.reviewcode" :disabled="true" placeholder="系统自动生成"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结项类型" prop="endtype" :rules="rules.validateRequire">
            <el-select v-model="model.endtype" filterable clearable @change="clear()" :disabled="true">
              <el-option v-for="item in codeModel.endtypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结项时间" prop="enddate" :rules="rules.validateRequire">
            <el-date-picker v-model="model.enddate" type="date" placeholder="例：2021-01-01" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否中标" prop="iswinbid" :rules="rules.validateRequire">
            <el-select v-model="model.iswinbid" filterable clearable @change="selectiswinbid">
              <el-option v-for="item in codeModel.iswinbidList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结项金额" prop="settlementmoney" :rules="rules.validateRequire">
            <el-input-number v-model="model.settlementmoney" controls-position="right" :step="1" :min="0" :precision="2" style="width:90%" :disabled="settlementmoneyable" @click.native="clicksettlementmoney"></el-input-number><span>万元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="项目成果" prop="achievements">
            <el-input v-model="model.achievements" type="textarea" :rows="4" maxlength="1000" show-word-limit></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="结项说明" prop="projectdescription">
            <el-input v-model="model.projectdescription" type="textarea" :rows="4" maxlength="1000" show-word-limit></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse>
        <el-collapse-item title="【单据】申请信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="applydeptname">
                <tl-ref-dept v-model="model.applydeptcode" :initDisplayData.sync="model.applydeptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请公司" prop="applyorgname">
                <el-input v-model="model.applyorgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人姓名" prop="applyusername">
                <tl-ref-user v-model="model.applyusername" :initDisplayData.sync="model.applyusername" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="applyusercode">
                <el-input v-model="model.applyusercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交时间" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="date" placeholder="提交时间" value-format="yyyy-MM-dd" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state">
                <el-input v-model="model.dispstate" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="MpmProjectReview" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="false">
        <svg-icon icon-class="save" />保存
      </el-button>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <tl-attachment sourceType="MpmProjectReview" :sourceId="model.id" :disabled="disableAttachment" :editable="true"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <tlmpm-project-ref ref="mpmProjectRef" @confirm="getmpmProjectRefData" :isSingleSelect="true" :externalQueryParam="externalQueryParamForPool"></tlmpm-project-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlmpmProjectRef from '@/views/mpm/mpmProject/refList'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlmpmProjectRef },
  data() {
    return {
      moduleTitle: '项目复盘结项',
      urlPrefix: '/mpm/projectReview/',
      externalQueryParamForPool: {},
      settlementmoneyable: false,
      // endtypeable: false,
      url: {
        // 额外的url
      }
    }
  },
  methods: {
    saveInfo(resolve, reject) {
      putAction(this.url.edit, this.model)
        .then(res => {
          if (res.success) {
            this.$emit('refresh')
            resolve()
          } else {
            this.$tap.alert(res.message, 'warning')
            reject()
          }
        })
        .catch(err => {
          this.$tap.alert('修改项目信息失败，请联系系统管理员', 'warning')
          reject()
        })
    },
    open() {
      this.model = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '新增' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then(_ => {
              this.assignDefaultValue()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        }
      })
    },
    clicksettlementmoney() {
      const { iswinbid } = this.model
      if (!iswinbid) {
        this.$tap.alert('请先选择是否中标！', 'warning')
        this.$set(this.model, 'settlementmoney', '')
      }
    },
    selectiswinbid() {
      const { iswinbid } = this.model
      this.settlementmoneyable = false
      if (iswinbid === '1') {
        this.settlementmoneyable = true
        this.$set(this.model, 'settlementmoney', 0)
        this.$emit('refresh')
      }
    },
    /** 打开项目参照 */
    openmpmProjectRefDialog() {
      const { applyusercode } = this.model
      this.externalQueryParamForPool = { qry_projectmanagercode: applyusercode, qry_projectstate: 1 }
      this.$refs.mpmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getmpmProjectRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'projectid', selectionRow.id)
      this.$set(this.model, 'projectname', selectionRow.projectname)
      this.$set(this.model, 'projectcode', selectionRow.projectcode)
      this.$set(this.model, 'deptcode', selectionRow.deptcode)
      this.aftergetproject()
    },
    aftergetproject() {
      const { projectid, projectcode } = this.model
      // this.endtypeable = false
      this.$set(this.model, 'endtype', '')
      var params = {
        qry_projectcode: projectcode,
        qry_taskstate: '0,1'
      }
      getAction('/mpm/projectTask/list', params).then(res => {
        if (res.success) {
          if (res.result.dataList.length > 0) {
            this.$tap.alert('该项目下有未完成的里程碑及任务和任务，只能异常结项！')
            this.$set(this.model, 'endtype', '1')
            // this.endtypeable = true
          } else {
            this.$set(this.model, 'endtype', '0')
          }
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    assignDefaultValue() {
      if (this.isAdd) {
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'applyusername', this.user.name)
        this.$set(this.model, 'applyusercode', this.user.code)
        this.$set(this.model, 'applydeptcode', this.dept.code)
        this.$set(this.model, 'applydeptname', this.dept.fullname)
        this.$set(this.model, 'applyorgcode', this.org.code)
        this.$set(this.model, 'applyorgname', this.org.name)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        // this.$set(this.model, 'deptcode', this.dept.code)
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    }
  }
}
</script>

